package com.sx.micowell.framework.model

import android.annotation.SuppressLint
import android.text.TextUtils
import android.util.Log
import com.example.mvvm.base.data.MutableLiveListData
import com.example.mvvm.base.vm.BaseViewModel
import com.example.mvvm.rx.RxBus
import com.example.mvvm.until.LogUtils
import com.example.mvvm.until.ToastUtils
import com.miloyu.modelnet.network.ApiResponse
import com.miloyu.modelnet.network.BaseResponse
import com.miloyu.modelnet.network.HttpClient
import com.miloyu.modelnet.network.tools.HttpsUtils
import com.miloyu.modelnet.network.tools.RxUtils
import com.miloyu.modelnet.request.AddTrainPlanDTO
import com.miloyu.modelnet.request.TrainPlanInfo
import com.miloyu.modelnet.response.CourseTypeListBean
import com.miloyu.modelnet.response.DictType
import com.miloyu.modelnet.response.PlanTypeListBean
import com.sx.micowell.framework.app.AppConstant
import com.sx.micowell.framework.arouth.RouteManager
import com.sx.micowell.framework.binding.BindingCommand
import com.sx.micowell.framework.binding.impl.IBindingAction
import com.sx.micowell.framework.tools.EventInfo
import io.reactivex.Observable
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers


/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/7/27 15:19
 */
class TrainPlanModel : BaseViewModel() {

    override fun registerRxBus() {
        super.registerRxBus()
        addSubscribe(
            RxBus.getDefault().toObservable(EventInfo::class.java)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { event ->
                    if (event.type == AppConstant.EVENT_ADD_TRAIN_SUCCESS) {
                        // 添加训练计划成功
                        context.finishEvent.call()
                    }
                }
        )
    }

    // 开始时间
    var selectStartTime: String? = null
    // 结束时间
    var selectEndTime: String? = null

    var trainTypeEvent : MutableLiveListData<DictType> = MutableLiveListData()

    // 点击事件
    val nextStepOnClickCommand = BindingCommand<Any>(IBindingAction {
        if (TextUtils.isEmpty(selectStartTime) || TextUtils.isEmpty(selectEndTime)) {
            ToastUtils.showShort("请选择开始时间和结束时间")
            return@IBindingAction
        }
        RouteManager.getInstance().intentSelectTrainType(selectStartTime, selectEndTime)
    })


    /**
     * 查询训练类型
     */
    @SuppressLint("CheckResult")
    fun queryTrainType() {

        HttpClient.instance.apiService
            .queryPlanType()
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<PlanTypeListBean>() {

                override fun success(data: PlanTypeListBean?, msg: String?) {
                    LogUtils.d("queryTrainType ${data?.dictType?.get(0)?.weeks!![0].dictLabel}")
                    trainTypeEvent.addData(data.dictType)
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    LogUtils.d("queryTrainType $errorMsg")
                }

            })

       

    }


    /**
     * 添加训练计划
     */
    @SuppressLint("CheckResult")
    fun addTrainPlan(dto: AddTrainPlanDTO) {

        HttpClient.instance.apiService
            .addTrainPlan(dto)
            .doOnSubscribe { ui.showDialog() }
            .compose(RxUtils.compose())
            .subscribeWith(object : ApiResponse<Void>() {

                override fun success(data: Void?, msg: String?) {
                    ToastUtils.showShort("添加成功")
                    ui.hideDialog()
                    RxBus.getDefault().post(EventInfo(AppConstant.EVENT_ADD_TRAIN_SUCCESS))
                }

                override fun onFail(e: Throwable?, errorMsg: String?) {
                    ui.hideDialog()
                }

            })
    }
}

